package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrEnum;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;


public class CSD extends AxdrType {
    private byte[] dataCode = null;
    private Choice choice = Choice._ERR_NONE_SELECTED;
    private OAD oad = null;
    private ROAD road = null;

    public CSD() {
    }

    public byte[] getDataCode() {
        return dataCode;
    }

    public Choice getChoice() {
        return choice;
    }

    public OAD getOad() {
        return oad;
    }

    public ROAD getRoad() {
        return road;
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setOad(OAD oad) {
        resetChoices();
        this.choice = Choice.OAD;
        this.oad = oad;
    }

    public void setRoad(ROAD road) {
        resetChoices();
        this.choice = Choice.ROAD;
        this.road = road;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        AxdrEnum choosen = new AxdrEnum();
        choosen.setConst();
        codeLength += choosen.decode(input);
        resetChoices();
        this.choice = Choice.getEnumByValue((int)choosen.getValue());
        switch (this.choice) {
            case OAD:
                this.oad = new OAD();
                codeLength += this.oad.decode(input);
                return codeLength;
            case ROAD:
                this.road = new ROAD();
                codeLength += this.road.decode(input);
                return codeLength;
            default:
                throw new Exception("Error decoding AxdrChoice: Identifier matched to no item.");
        }
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        if (this.dataCode != null) {
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
            return this.dataCode.length;
        }
        if (this.choice == Choice._ERR_NONE_SELECTED) {
            throw new Exception("Error encoding AxdrChoice: No item in choice was selected.");
        }
        int codeLength = 0;
        AxdrEnum c = new AxdrEnum();
        switch (this.choice) {
            case ROAD:
                codeLength += this.road.encode(output);
                c.setMinMaxVal(1);
                codeLength += c.encode(output);
                return codeLength;
            case OAD:
                codeLength += this.oad.encode(output);
                c.setMinMaxVal(0);
                codeLength += c.encode(output);
                return codeLength;
            default:
                throw new Exception("Error encoding AxdrChoice: No item in choice was encoded.");
        }
    }

    public Choice getChoiceIndex() {
        return this.choice;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.toByteArray();
    }

    public void resetChoices() {
        this.choice = Choice._ERR_NONE_SELECTED;
        this.oad = null;
        this.road = null;
    }

    @Override
    public String toString() {
        switch (this.choice) {
            case OAD:
                return this.oad != null ? "choice: {oad: " + this.oad + "}" : "choice is oad but oad is null";
            case ROAD:
                return this.road != null ? "choice: {road: " + this.road + "}" : "choice is road but road is null";
            default:
                return "unknown";
        }
    }

    public enum Choice {
        _ERR_NONE_SELECTED(-1),
        OAD(0),
        ROAD(1);

        private final int value;

        Choice(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static Choice getEnumByValue(int value) {
            for (Choice item : Choice.values()) {
                if (value == item.getValue()) {
                    return item;
                }
            }
            return null;
        }
    }
}



